<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>店面群发给客户管理</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		body {overflow:scroll;}
		#tip-msg{
		 margin-left: 95px;
		 margin-right: 100px;
		}
		#selectCustomer{
			height: 100px;
			overflow:auto;
			border: 1px solid;
			border-color: #ccc;
			border-radius:5px;
		}
		#msgContent{
			border: 1px solid #777b12;
		}
	</style>
</head>
<body>
	<div class="icon-buttons">
		<input type="button" class="leave-not-clean-session" data-inline="true" data-icon="back" data-iconpos="notext" value="返回"/>
	</div>
	<form:form id="inputForm" modelAttribute="smsStoreGroupToCustomer" action="${ctx}/sms/sendStoreGroupToCustomer/save" method="post" class="form-input">
		<tags:message content="${message}"/>
		<input type="hidden" name="isEmployee" value="true"/>
		<input type="hidden" id="target" name="target" value="${target}"/>
		<input type="hidden" id="sendType" name="sendType" value="false"/>
		<input type="hidden" id="delRealtionship" name="delRealtionship" value="true"/>
		<div class="middleCls">
			<input id="btnSubmit" type="submit"  data-inline="true"  value="保存并发送"/>
			<input id="sendBtn" type="submit" data-inline="true"  value="立即发送"/>
		</div>
		<label style="color:red">提示:</label>短信默认发送的时间段为14:00~17:00之间立即发送，如果不在此时间段内，短信将被保存，在规定的时间段（14:00~17:00）自动发送，短信发送后将自动接触该员工与其负责的客户的关系。
		<div data-role="fieldcontain">
			<label for="employeeIds" class="select">离职员工选择:</label>
			<select data-native-menu="false" name="employeeIds" id="employeeIds">
				<option value="">---请选择---</option>
				<c:forEach items="${employeeList}" var="employee">
					<option value="${employee.id}">${employee.name}</option>
				</c:forEach>
			</select>
		</div>
		<div data-role="fieldcontain">
			<label for="customerIds">客户选择:</label>
			<select data-native-menu="false" name="customerIds" multiple="multiple"  id="customerIds" >
			</select>
		</div>
		<div data-role="fieldcontain">
			<label for="selectCustomer">已选客户:</label>
			<div id="selectCustomer"></div>
		</div>
		<div data-role="fieldcontain">
			<label for="msgContent">内容:</label>
			<textarea id="msgContent" name="msgContent" maxlength="512" rows="5" cols="50" ></textarea>
		</div>
	</form:form>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#inputForm").validate({
				submitHandler: function(form){
					var customers = $("#customerIds").val();
					if (!customers){
						showTip("请选择接收信息的客户！");
						return;
					}
					var msg = $("#msgContent").val();
					msg = msg.replace(/[\r\n]/g,"").replace(/^\s*/g,"");
					if (!msg){
						showTip("请输入要发送的内容！");
						return;
					}
					$("#msgContent").val(msg);
					var submit = function(){
						sendMessage(form, 'sms', $("#sendType").val());
		            };
		    		loginPasswordPopup(submit);
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			$("#employeeIds").change(function(){
				var eid = $("#employeeIds").val();
				findCustomerByEmployee("${ctx}", eid);
			});
			$("#customerIds").change(function(){
				var sel = "";
				$("#customerIds").find("option").each(function(id,op){
					if (op.selected) {
						sel += op.text +"<br/>";
						}
				 });  
				$("#selectCustomer").html(sel);
			});
			$("#sendBtn").click(function(){
				$("#sendType").val('true');
			});
			$('#delRelationBtn').click(function(){ 
				var id = $("#employeeSelect").val(); 
				if (id == 0) {
					showTip("未选择员工！");
					return false;
				}
				var fun = function(){
					deleteRelationshipBetweenCustomerAndEmployee('${ctx}', id);
				};
				global.confirm("确定要解除此员工和客户的所有关联",fun);
				return false;
			});
		});
		//解除顾客和员工直接的关系
		function deleteRelationshipBetweenCustomerAndEmployee(ctx, employeeId){
			jQuery.ajax({  
				type : 'GET',  
				url : ctx + "/employee/employee/delRealtionship?employeeId=" + employeeId,  
				success : function(data) { 
				  var dataJson = eval("("+data+")");
				  if (dataJson && dataJson.sum > 0) {
					  showTip("成功解除此员工与" + dataJson.sum + "位客户的关联！");
				  } else {
					  showTip("此员工与客户不存在关联！");
				  }
				}  
			});
		}
	</script>
</body>
</html>
